/**
 * BeautifyObserver.java
 */
package com.pluginsacceptor.plugin.beautify;

import java.util.Observable;
import java.util.Observer;

import org.apache.log4j.Logger;

import com.pluginsacceptor.plugin.beautify.model.CellList;
import com.pluginsacceptor.utils.CommunicationUtils;

/**
 * The class implements action for updating spread sheet components
 */
public class BeautifyObserver implements Observer {

    /** Logger */
    private static final Logger LOG = Logger.getLogger(BeautifyObserver.class);

    /** */
    private final CellList cellList = new CellList();

    /**
     * {@inheritDoc}
     */
    @Override
    public void update(final Observable arg0, final Object externalData) {

        if (externalData instanceof String) {
            final String cellString = (String) externalData;
            try {
                parseData(cellString);
            } catch (final Exception e) {
                LOG.warn("Unexpected message format '" + cellString + "'", e);
            }
        }
    }

    /**
     * Parse source data string
     */
    private void parseData(final String cellString) {
        LOG.debug("Received data '" + cellString + "'");
        if (CommunicationUtils.isStartCellSelection(cellString)) {
            this.cellList.clear();

//        } else if (CommunicationUtils.isEndCellSelection(cellString)) {

        } else if (
            CommunicationUtils.isCellData(cellString) && !CommunicationUtils.isEmptyCell(cellString)
        ) {
            this.cellList.add(
                CommunicationUtils.getRowNumFromCellString(cellString),
                CommunicationUtils.getColNumFromCellString(cellString)
            );
//            LOG.debug(
//                "Received data[ col: " + CommunicationUtils.getColNumFromCellString(cellString)
//                    + ", row: " + CommunicationUtils.getRowNumFromCellString(cellString)
//                    + ", value: " + CommunicationUtils.getValueFromCellString(cellString)
//            );
        }
    }

    /**
     * @return the Active Cell List
     */
    public CellList getActiveCellList() {
        return cellList;
    }



}
